#include "PythonModule.hpp"



#include <Python.h>
#include "structmember.h"



PyDoc_STRVAR(mymodule__doc__,"mymodule is a new object for python.\n");

#define DEFERRED_ADDRESS(ADDR) 0
#if 0
typedef struct {
    PyObject object;
    int state;
    int value;
} myObject;


static PyObject* myObject_getstate(myObject *self, PyObject* args) {
	if (!PyArg_ParseTuple(args,":getstate"))
		return NULL;
	return PyInt_FromLong(self->state);
};

static PyObject* myObject_setstate(myObject *self, PyObject* args) {
	int state;

	if (!PyArg_ParseTuple(args, "i:setstate", &state)) return NULL;
	
	self->state = state;
	Py_INCREF(Py_None);
	return Py_None;
}



static PyMethodDef myObject_methods[] = {
	{"getstate", (PyCFunction)myObject_getstate, METH_VARARGS, PyDoc_STR("getstate() -> state")},
	{"setstate", (PyCFunction)myObject_setstate, METH_VARARGS, PyDoc_STR("setstate(state)")},
	{NULL,NULL}
};


static int myObject_init (PyObject* self, PyObject* args, PyObject* kwds) {
	// init ???
	((myObject*) self)->state = 0;
	return 0;
}


static PyObject* myObject_state_get(myObject* self) {
	return PyInt_FromLong(self->state);
}

static int myObject_state_set(myObject* self, PyObject* args) {
	int state;
	// TODO : check args is int ?
	self->state = PyInt_AsLong(args);
	return 0;
}

static PyGetSetDef myObject_getsets[] = {
	{"state", (getter) myObject_state_get, (setter) myObject_state_set,
	PyDoc_STR("int variable storing state")},
	{0}
};


static PyTypeObject myObject_type = {
	PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
	"mymodule.myObject",
	sizeof(myObject),
	0,
	0,					/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
	0,					/* tp_repr */
	0,					/* tp_as_number */
	0,					/* tp_as_sequence */
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
	0,					/* tp_getattro */
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT , /* tp_flags */
	0,					/* tp_doc */
	0,					/* tp_traverse */
	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	myObject_methods,			/* tp_methods */
	0,					/* tp_members */
	myObject_getsets,			/* tp_getset */
	0,		/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	myObject_init,		/* tp_init */
	PyType_GenericAlloc,					/* tp_alloc */
	PyType_GenericNew,					/* tp_new */
};

static PyMethodDef mymodule_functions[] = { {NULL,NULL}}; 

#include "pythonCModule/pythonGallionObject.hpp"
#include "pythonCModule/pythonHeightMap.hpp"
#include "pythonCModule/pythonDecor.hpp"

#endif

void PythonModule::init() {
	Py_Initialize();
	// initmyObjectsubtype();
	PyObject* m;
	// subtype init, needed for finalizing subtype init
	/*if (PyType_Ready(&myObject_type) < 0) return;
	if (PyType_Ready(&GallionObject_type) < 0) return;
	if (PyType_Ready(&HeightMap_type) < 0) return;
	if (PyType_Ready(&Palmier_type) < 0) return;

	m = Py_InitModule3("mymodule",mymodule_functions, mymodule__doc__);
	
	if (m == NULL) return;

	Py_INCREF(&myObject_type);
	Py_INCREF(&GallionObject_type);
	Py_INCREF(&HeightMap_type);
	Py_INCREF(&Palmier_type);

	if (PyModule_AddObject(m, "myObject", (PyObject*) & myObject_type) < 0) return;
	if (PyModule_AddObject(m, "Gallion", (PyObject*) & GallionObject_type) < 0) return;
	if (PyModule_AddObject(m, "HeightMap", (PyObject*) & HeightMap_type) < 0) return;
	if (PyModule_AddObject(m, "Palmier", (PyObject*) & Palmier_type) < 0) return;*/
}
void PythonModule::runString(string command) {
	const char* cmd_ = command.c_str();
	cout << "Rcmd #" << cmd_ << "#" << endl;
	PyRun_SimpleString(cmd_);
};
#if 0
int main() {
	PythonModule::init();
	cout << "salut" << endl;
	PythonModule::runString("print \"bonjour\"");
	return 0;

}
#endif
